package com.iussoft.demo.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;

/**
 * @description:
 * @author: Hou Dayu
 * @date: Created in 2020/10/27
 */
public class ConfigUtil {

    private static Properties config = null;
    public static String WSPORT;
    public static String ISAUTOIP="true";

    public static Properties initWS(File wsFile) {
        config = new Properties();
        try {
            config.load(new InputStreamReader(new FileInputStream(wsFile), "utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return config;
    }

    public static String getConfigByKey(String key){
        return config.getProperty(key);
    }

    public static String getWsIp() {
        String is_auto_ip = ISAUTOIP;
        String ip = "";
        if (is_auto_ip.equals("true")) {
            try {
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                System.out.println("读取本机ip地址报错！");
                e.printStackTrace();
            }
        }
        if ((ip == null) || (ip.equals(""))) {
            ip = "127.0.0.1";
        }
        return ip;
    }

    public static String getWsPort(){
        String port = WSPORT;
        if ((port == null) || (port.equals(""))) {
            port = "9999";
        }
        return port;
    }

    public static String getWsIpAndPort(){
        String ip = getWsIp();
        String port = getWsPort();
        return "http://" + ip + ":" + port;
    }
}
